package com.zc;


import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.zc.utils.Modbus4jReadUtils;
import com.zc.utils.Modbus4jWriteUtils;

/**
 * @description:
 * @author: cong
 * @create: 2025/1/3
 */
public class ModbusTest {
    public static void main(String[] args) throws Exception {
        Number number = Modbus4jReadUtils.readHoldingRegister(1, 0, DataType.TWO_BYTE_INT_SIGNED);
        System.out.println("number: " + number);

        short[] shorts = Modbus4jReadUtils.readHoldingRegisters(1, 0, 2);
        for (short s : shorts) {
            System.out.println("short: " + s);
        }

        boolean isSuccess = Modbus4jWriteUtils.writeRegister(1, 0, (short) 100);
        System.out.println("isSuccess: " + isSuccess);

        boolean isArraySuccess = Modbus4jWriteUtils.writeRegisters(1, 0, new short[]{101, 102});
        System.out.println("isArraySuccess: " + isArraySuccess);

        boolean isWriteCoilResult = Modbus4jWriteUtils.writeCoilStatus(1, 0, true);
        System.out.println("isWriteCoilResult: " + isWriteCoilResult);

        Modbus4jWriteUtils.writeHoldingRegister(1, 0, 55, DataType.TWO_BYTE_INT_SIGNED);
    }
}
